{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8a9e565d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.feature_selection import mutual_info_regression\n",
    "from sklearn.svm import SVR\n",
    "import pandas as pd\n",
    "from sklearn.neural_network import MLPRegressor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ac44e903",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data = pd.read_excel('./data.xlsx', index_col='年份')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "62d419cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>物流货运量\\n单位：万吨</th>\n",
       "      <th>居民人均可支\\n配收入（元）</th>\n",
       "      <th>地区生产总值</th>\n",
       "      <th>第一产业总值</th>\n",
       "      <th>第二产业总值</th>\n",
       "      <th>第三产业总值</th>\n",
       "      <th>社会消费品\\n零售总额</th>\n",
       "      <th>年末常住人口\\n单位：万人</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>年份</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>54943.0</td>\n",
       "      <td>4000.120</td>\n",
       "      <td>3928.20</td>\n",
       "      <td>945.58</td>\n",
       "      <td>1433.11</td>\n",
       "      <td>1549.51</td>\n",
       "      <td>1669.30</td>\n",
       "      <td>8234.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>54141.0</td>\n",
       "      <td>4173.735</td>\n",
       "      <td>4293.49</td>\n",
       "      <td>981.67</td>\n",
       "      <td>1572.01</td>\n",
       "      <td>1739.81</td>\n",
       "      <td>1877.55</td>\n",
       "      <td>8143.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>57297.0</td>\n",
       "      <td>4359.500</td>\n",
       "      <td>4725.01</td>\n",
       "      <td>1047.95</td>\n",
       "      <td>1733.38</td>\n",
       "      <td>1943.68</td>\n",
       "      <td>2066.85</td>\n",
       "      <td>8110.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003</th>\n",
       "      <td>57200.0</td>\n",
       "      <td>4636.000</td>\n",
       "      <td>5346.20</td>\n",
       "      <td>1128.57</td>\n",
       "      <td>2020.50</td>\n",
       "      <td>2197.13</td>\n",
       "      <td>2289.71</td>\n",
       "      <td>8176.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>65580.0</td>\n",
       "      <td>5145.000</td>\n",
       "      <td>6303.96</td>\n",
       "      <td>1329.07</td>\n",
       "      <td>2439.71</td>\n",
       "      <td>2535.18</td>\n",
       "      <td>2621.15</td>\n",
       "      <td>8090.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      物流货运量\\n单位：万吨  居民人均可支\\n配收入（元）    地区生产总值   第一产业总值   第二产业总值   第三产业总值  \\\n",
       "年份                                                                        \n",
       "2000       54943.0         4000.120  3928.20   945.58  1433.11  1549.51   \n",
       "2001       54141.0         4173.735  4293.49   981.67  1572.01  1739.81   \n",
       "2002       57297.0         4359.500  4725.01  1047.95  1733.38  1943.68   \n",
       "2003       57200.0         4636.000  5346.20  1128.57  2020.50  2197.13   \n",
       "2004       65580.0         5145.000  6303.96  1329.07  2439.71  2535.18   \n",
       "\n",
       "      社会消费品\\n零售总额  年末常住人口\\n单位：万人  \n",
       "年份                                \n",
       "2000      1669.30         8234.8  \n",
       "2001      1877.55         8143.0  \n",
       "2002      2066.85         8110.0  \n",
       "2003      2289.71         8176.0  \n",
       "2004      2621.15         8090.0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98f7de66",
   "metadata": {},
   "source": [
    "# 检查缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6ba55dd",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 21 entries, 2000 to 2020\n",
      "Data columns (total 8 columns):\n",
      " #   Column          Non-Null Count  Dtype  \n",
      "---  ------          --------------  -----  \n",
      " 0   物流货运量\n",
      "单位：万吨     21 non-null     float64\n",
      " 1   居民人均可支\n",
      "配收入（元）   21 non-null     float64\n",
      " 2   地区生产总值          21 non-null     float64\n",
      " 3   第一产业总值          21 non-null     float64\n",
      " 4   第二产业总值          21 non-null     float64\n",
      " 5   第三产业总值          21 non-null     float64\n",
      " 6   社会消费品\n",
      "零售总额      21 non-null     float64\n",
      " 7   年末常住人口\n",
      "单位：万人    21 non-null     float64\n",
      "dtypes: float64(8)\n",
      "memory usage: 1.5 KB\n"
     ]
    }
   ],
   "source": [
    "info = raw_data.info()\n",
    "des = raw_data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "64c5f07f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['物流货运量\\n单位：万吨', '居民人均可支\\n配收入（元） ', '地区生产总值', '第一产业总值', '第二产业总值',\n",
       "       '第三产业总值', '社会消费品\\n零售总额', '年末常住人口\\n单位：万人'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "005c66e7",
   "metadata": {},
   "source": [
    "# 互信息特征提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "71aaaea2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('第二产业总值', 1.0892551522306066),\n",
       " ('第一产业总值', 1.0354380698420949),\n",
       " ('地区生产总值', 0.9978307036633005),\n",
       " ('社会消费品\\n零售总额', 0.9667649440261123),\n",
       " ('居民人均可支\\n配收入（元） ', 0.9608692524161351),\n",
       " ('第三产业总值', 0.9031246161000701),\n",
       " ('年末常住人口\\n单位：万人', 0.09227514810774462)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = raw_data.copy()\n",
    "y = X.pop('物流货运量\\n单位：万吨')\n",
    "info = mutual_info_regression(X, y)\n",
    "mi = []\n",
    "for i in range(len(info)):\n",
    "    mi.append((X.columns[i], info[i]))\n",
    "mi.sort(key = lambda x: x[1], reverse=True)\n",
    "mi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7e51030e",
   "metadata": {},
   "outputs": [],
   "source": [
    "col_drop = ['年末常住人口\\n单位：万人'] # 丢弃无关变量\n",
    "process_data = X.drop(col_drop, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1402180",
   "metadata": {},
   "source": [
    "# 引入支持向量回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "eb4bcd0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "svr = SVR(kernel = \"linear\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5981edae",
   "metadata": {},
   "source": [
    "# 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "433bef10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-19 {color: black;}#sk-container-id-19 pre{padding: 0;}#sk-container-id-19 div.sk-toggleable {background-color: white;}#sk-container-id-19 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-19 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-19 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-19 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-19 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-19 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-19 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-19 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-19 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-19 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-19 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-19 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-19 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-19 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-19 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-19 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-19 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-19 div.sk-item {position: relative;z-index: 1;}#sk-container-id-19 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-19 div.sk-item::before, #sk-container-id-19 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-19 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-19 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-19 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-19 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-19 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-19 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-19 div.sk-label-container {text-align: center;}#sk-container-id-19 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-19 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-19\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>SVR(kernel=&#x27;linear&#x27;)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-19\" type=\"checkbox\" checked><label for=\"sk-estimator-id-19\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">SVR</label><div class=\"sk-toggleable__content\"><pre>SVR(kernel=&#x27;linear&#x27;)</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "SVR(kernel='linear')"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svr.fit(process_data, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c43b25b9",
   "metadata": {},
   "source": [
    "# 计算误差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "9303763c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_absolute_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "f1a51225",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_svr = svr.predict(process_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "9fcdbb5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "ma_svr = mean_absolute_error(y, y_pred_svr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "7aa47646",
   "metadata": {},
   "outputs": [],
   "source": [
    "ma_svr_loss_rate = ma_svr / np.std(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "fd57b5df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "误差: 5341\n",
      "误差率: 10.81%\n"
     ]
    }
   ],
   "source": [
    "print(\"误差: %d\"%(ma_svr)) # 误差\n",
    "print(\"误差率: %.2f\"%(ma_svr_loss_rate*100) + '%') # 误差率"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
